home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Libraries / MacWT 0.04 / wt / world.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  2.8 KB  |  117 lines  |  [TEXT/MMCC]

  1. /*
  2. **  wt -- a 3d game engine
  3. **
  4. **  Copyright (C) 1994 by Chris Laurel
  5. **  email:  claurel@mr.net
  6. **  snail mail:  Chris Laurel, 5700 W Lake St #208,  St. Louis Park, MN  55416
  7. **
  8. **  This program is free software; you can redistribute it and/or modify
  9. **  it under the terms of the GNU General Public License as published by
  10. **  the Free Software Foundation; either version 2 of the License, or
  11. **  (at your option) any later version.
  12. **
  13. **  This program is distributed in the hope that it will be useful,
  14. **  but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. **  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. **  GNU General Public License for more details.
  17. **
  18. **  You should have received a copy of the GNU General Public License
  19. **  along with this program; if not, write to the Free Software
  20. **  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22.  
  23. #include <stdlib.h>
  24. #include <stdio.h>
  25. #include <math.h>
  26. #include "wt.h"
  27. #include "error.h"
  28. #include "fixed.h"
  29. #include "wtmem.h"
  30. #include "texture.h"
  31. #include "table.h"
  32. #include "world.h"
  33.  
  34.  
  35. #define START_VERTICES 50
  36. #define START_WALLS    30
  37. #define START_TEXTURES 10
  38.  
  39.  
  40. World *new_world(void)
  41. {
  42.      World *w;
  43.  
  44.      w = wtmalloc(sizeof(World));
  45.      w->vertices = new_table(sizeof(Vertex));
  46.      w->walls = new_table(sizeof(Wall));
  47.      w->regions = new_table(sizeof(Region));
  48.      w->textures = new_table(sizeof(Texture *));
  49.  
  50.      return w;
  51. }
  52.  
  53.  
  54. void add_texture(World *w, Texture *tex)
  55. {
  56.      add_table_entry(w->textures, &tex);
  57. }
  58.  
  59.  
  60. void add_wall(World *w, Wall *wall)
  61. {
  62.      add_table_entry(w->walls, wall);
  63. }
  64.  
  65.  
  66. void add_vertex(World *w, Vertex *v)
  67. {
  68.      add_table_entry(w->vertices, v);
  69. }
  70.  
  71.  
  72. void add_region(World *w, Region *r)
  73. {
  74.      add_table_entry(w->regions, r);
  75. }
  76.  
  77.  
  78. void update_wall_scale(World *w, int wall_num, fixed xscale, fixed yscale)
  79. {
  80.      Wall *wall;
  81.      Texture *texture;
  82.      fixed wall_length;
  83.  
  84.      if (wall_num < 0 || wall_num > TABLE_SIZE(w->walls))
  85.       fatal_error("invalid wall number");
  86.  
  87.      wall = (Wall *) w->walls->table + wall_num;
  88.      texture = wall->surface_texture;
  89.  
  90.      wall_length =
  91.       FLOAT_TO_FIXED(sqrt(FIXED_TO_FLOAT(wall->vertex2->x -
  92.                          wall->vertex1->x) *
  93.                   FIXED_TO_FLOAT(wall->vertex2->x -
  94.                          wall->vertex1->x) +
  95.                   FIXED_TO_FLOAT(wall->vertex2->y -
  96.                          wall->vertex1->y) *
  97.                   FIXED_TO_FLOAT(wall->vertex2->y -
  98.                          wall->vertex1->y)));
  99.      wall->yscale = fixmul(yscale, INT_TO_FIXED(texture->height));
  100.      wall->xscale = fixmul(fixmul(xscale, INT_TO_FIXED(texture->width)),
  101.                wall_length);
  102. }
  103.  
  104.  
  105. void update_wall_phase(World *w, int wall_num, fixed xphase, fixed yphase)
  106. {
  107.      Wall *wall;
  108.  
  109.      if (wall_num < 0 || wall_num > TABLE_SIZE(w->walls))
  110.       fatal_error("invalid wall number");
  111.  
  112.      wall = (Wall *) w->walls->table + wall_num;
  113.  
  114.      wall->xphase = xphase;
  115.      wall->yphase = yphase;
  116. }
  117.